﻿namespace Home.Http
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json.Linq;

    internal static class HttpHelper
    {
        public static async Task<HttpJsonResult> GetJsonAsync(string uri)
        {
            Debug.Assert(!string.IsNullOrEmpty(uri));

            var header = default(string);
            var body = default(string);
            var json = default(JToken);

            using (var response = await GetAsync(uri))
            {
                if (response == null)
                {
                    return null;
                }

                header = response.Headers.ToString();
                body = await response.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(body))
                {
                    return null;
                }

                json = JToken.Parse(body);
            }

            return new HttpJsonResult()
            {
                Header = header,
                Body = body,
                Json = json,
            };
        }

        public static async Task<HttpBinaryResult> GetBinaryAsync(string uri)
        {
            Debug.Assert(!string.IsNullOrEmpty(uri));

            var header = default(string);
            var body = default(byte[]);

            using (var response = await GetAsync(uri))
            {
                if (response == null)
                {
                    return null;
                }

                header = response.Headers.ToString();
                body = await response.Content.ReadAsByteArrayAsync();
            }

            return new HttpBinaryResult()
            {
                Header = header,
                Body = body,
            };
        }

        public static async Task<HttpStringResult> GetStringAsync(string uri)
        {
            Debug.Assert(!string.IsNullOrEmpty(uri));

            var header = default(string);
            var body = default(string);

            using (var response = await GetAsync(uri))
            {
                if (response == null)
                {
                    return null;
                }

                header = response.Headers.ToString();
                body = await response.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(body))
                {
                    return null;
                }
            }

            return new HttpStringResult()
            {
                Header = header,
                Body = body,
            };
        }

        private static async Task<HttpResponseMessage> GetAsync(string uri)
        {
            using (var http = new HttpClient())
            using (var response = await http.GetAsync(uri, HttpCompletionOption.ResponseContentRead))
            {
                if (!response.IsSuccessStatusCode)
                {
                    return null;
                }

                return response;
            }
        }
    }
}
